home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Scope / Scope Disk #213 (199x)(Scope PD)(US)[WB].zip / Scope Disk #213 (199x)(Scope PD)(US)[WB].adf / LAUNCH / LAUNCH.C < prev    next >
C/C++ Source or Header  |  1978-10-24  |  6KB  |  195 lines

  1. /* Launch.c - © 1991 Arnie Cachelin, HyperActive InterMedia       */
  2. /* execute() commands from customized buttons */
  3. /* 08 Aug 1991 At 20:34:49 */
  4. /* Many of the following includes are actually useful... i hope */
  5. #include <proto/intuition.h>
  6. #include <proto/gadtools.h>
  7. #include <proto/exec.h>
  8. #include <intuition/gadgetclass.h>
  9. #include <intuition/screens.h>
  10. #include <workbench/startup.h>
  11. #include <workbench/workbench.h>
  12. #include <workbench/icon.h>
  13.  
  14. #define DEFAULT_LENGTH 12
  15. #define DEFAULT_NUMBER 1
  16. #define MIN_LENGTH 4
  17. #define MAX_NUMBER 9
  18. #define MAX_LENGTH 20
  19. #define XMARGIN 8
  20. #define YMARGIN 8
  21.  
  22. struct IconBase     *IconBase=NULL;
  23. struct IntuitionBase *IntuitionBase = NULL;
  24. struct GadToolsBase *GadToolsBase = NULL;
  25. struct Window *WBWindow = NULL;
  26. struct Screen *WBScreen = NULL;
  27. APTR vi = NULL;
  28. extern struct WBStartup   *WBenchMsg;
  29. struct  DiskObject *dob = NULL;
  30. struct Gadget *currgad = NULL;
  31. struct Gadget *gadlist = NULL;
  32. char  *txt,*cmd[MAX_NUMBER];
  33.  
  34. struct NewGadget ng = {
  35.     15, 25,
  36.     165, 95,
  37.     "",
  38.     NULL,
  39.     1,
  40.     PLACETEXT_IN,
  41.     NULL,
  42.     NULL
  43. };
  44.  
  45. Terminate(int returnvalue)
  46. {
  47.     if(dob) FreeDiskObject(dob);
  48.     if (gadlist) FreeGadgets(gadlist);
  49.     if (vi) FreeVisualInfo(vi);
  50.     if (WBWindow) CloseWindow (WBWindow);
  51.     if (WBScreen != NULL) UnlockPubScreen (NULL, WBScreen);
  52.     if (GadToolsBase) CloseLibrary ((struct Library *)GadToolsBase);
  53.     if(IconBase) CloseLibrary((struct Library *)IconBase);
  54.     if (IntuitionBase) CloseLibrary ((struct Library *)IntuitionBase);
  55.     exit (returnvalue);
  56.     return(0);
  57. }
  58.  
  59. void Bummer(char *errmsg)
  60. {
  61.     puts(errmsg);
  62.     Terminate(5);
  63. }
  64.  
  65. void Initialize()
  66. {
  67.   UBYTE   namebuf[MAXPUBSCREENNAME];
  68.     IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library", 36);
  69.     if (IntuitionBase == NULL)
  70.         Terminate(30);
  71.  
  72.     IconBase = (struct IconBase *) OpenLibrary ("icon.library", 36);
  73.     if (IconBase == NULL)
  74.         Terminate(30);
  75.  
  76.     GadToolsBase = (struct GadToolsBase *) OpenLibrary ("gadtools.library", 36);
  77.     if (GadToolsBase == NULL) Terminate(30);
  78.     GetDefaultPubScreen(namebuf);  /* Use namebuf=NULL for Workbench screen!  */
  79.     WBScreen = LockPubScreen(namebuf);
  80.     if (WBScreen == NULL) Terminate(30);
  81.  
  82.     vi = GetVisualInfo (WBScreen, TAG_DONE);
  83.     if (vi == NULL)
  84.         Terminate(30);
  85.  
  86.     ng.ng_VisualInfo = vi;
  87.     currgad = CreateContext (&gadlist);
  88.     ng.ng_TextAttr = WBScreen->Font;
  89.         ng.ng_Height = (WBScreen->Font->ta_YSize *4)/3;
  90. }
  91.  
  92. void handleIDCMP(struct Window *win)
  93. {
  94.     struct IntuiMessage *message = NULL;
  95.     struct Gadget *gad = NULL;
  96.     ULONG class, signals, signalmask;
  97.     WORD code;
  98.  
  99.     signalmask = 1L << win->UserPort->mp_SigBit;
  100.     while (1)
  101.     {
  102.         signals = Wait(signalmask);
  103.         if (signals & signalmask)
  104.             while (message = (struct IntuiMessage *)GT_GetIMsg(win->UserPort))
  105.             {
  106.                 class = message->Class;
  107.                 code = (WORD)message->Code;
  108.                 gad = (struct Gadget *) message->IAddress;
  109.                 GT_ReplyIMsg(message);
  110. /*                                 printf("Gadget: %d,  Code: %d\n",gad->GadgetID,code); */
  111.                 switch(class)
  112.                 {
  113.                     case CLOSEWINDOW:
  114.                        Terminate(0);
  115.                     case GADGETUP:
  116.                       Execute(cmd[gad->GadgetID],NULL,NULL);
  117.                                     default:
  118.                        break;
  119.                 }
  120.             }
  121.     }
  122. }
  123.  
  124. struct Gadget *AddButton(int x,int y,char *txt,int id)
  125. {
  126.          ng.ng_GadgetText=txt;
  127.          ng.ng_TopEdge= y;
  128.          ng.ng_LeftEdge= x;
  129.          ng.ng_GadgetID= id;
  130.     return(currgad=CreateGadget(BUTTON_KIND, currgad, &ng, TAG_DONE));
  131. }
  132.  
  133. void main(int argc, char *argv[])
  134. {
  135.     int   width=DEFAULT_LENGTH,count=DEFAULT_LENGTH,TlType=0,c,
  136.           cols,rows,x=XMARGIN,y=YMARGIN;
  137.     char  *name;
  138.  
  139.     Initialize();
  140.     if(argc)   /* If CLI start, get icon anyway... */
  141.       dob = (struct  DiskObject *)GetDiskObject(name=argv[0]);
  142.     else        /* Started from Workbench */
  143.        /* Get program icon using name in WB Startup Msg */
  144.       dob=(struct  DiskObject *)GetDiskObject(name=WBenchMsg->sm_ArgList->wa_Name);
  145.     if (dob==0) Bummer("No Icon Found!");
  146.  
  147.     if (txt=(char *)FindToolType(dob->do_ToolTypes,"WIDTH"))
  148.     {   /* Check user supplied text length  */
  149.        if ( !(width=atol(txt)) || width<MIN_LENGTH ) width=DEFAULT_LENGTH;
  150.        else if (width>MAX_LENGTH) width=MAX_LENGTH;
  151.        TlType++;
  152.     }
  153.     else width=DEFAULT_LENGTH;
  154.     width *= WBScreen->RastPort.TxWidth;
  155.     ng.ng_Width = width;
  156.  
  157.     if (txt=(char *)FindToolType(dob->do_ToolTypes,"COUNT"))
  158.     {   /* Check user supplied text NUMBER, 1 is always MIN */
  159.        if ( !(count=atol(txt)) ) count=DEFAULT_NUMBER;
  160.        else if (count>MAX_NUMBER) count=MAX_NUMBER;
  161.        TlType++;
  162.     }
  163.     else count=DEFAULT_NUMBER;
  164.  
  165.     if (count>8) cols = 3;              /* How to arrange these buttons..  */
  166.     else if (count>5) cols=(count+1)>>1;
  167.     else cols=count;
  168.     rows=(count%cols ? count/cols + 1: count/cols);    /* 7 is the pain... */
  169.  
  170.     for(c=0;c<count && dob->do_ToolTypes[TlType] ;c++)  /* Loop through ttypes  */
  171.     {
  172.       x=XMARGIN + (c%cols)*(width+XMARGIN);
  173.       y=YMARGIN + (c/cols)*(ng.ng_Height+YMARGIN);
  174.       txt=dob->do_ToolTypes[TlType++];
  175.       if (dob->do_ToolTypes[TlType]) cmd[c]=(char *)dob->do_ToolTypes[TlType++];
  176.       else cmd[c]=""; /*  This may not be necessary...  */
  177.       if(!AddButton(x,y,txt,c)) Bummer("Cant Add That One!");
  178.     }
  179.  
  180.     WBWindow = OpenWindowTags (NULL, WA_Title, (ULONG)name,
  181.         WA_Flags, (ULONG)SMART_REFRESH|WINDOWCLOSE|WINDOWDEPTH|WINDOWDRAG|ACTIVATE,
  182.         WA_InnerHeight, YMARGIN + rows*(ng.ng_Height+YMARGIN),
  183.         WA_InnerWidth, cols*(width+XMARGIN)+XMARGIN,
  184.         WA_AutoAdjust, TRUE,
  185.         WA_GimmeZeroZero, TRUE,
  186.         WA_IDCMP, (ULONG)CLOSEWINDOW|BUTTONIDCMP,
  187.         TAG_DONE);
  188.  
  189.     AddGList (WBWindow, gadlist, -1, -1, NULL);
  190.     RefreshGList(WBWindow->FirstGadget, WBWindow, NULL, (UWORD)-1);
  191.     GT_RefreshWindow (WBWindow, NULL);
  192.     handleIDCMP (WBWindow);
  193. }
  194.  
  195.